package com.intellagric.service.sys_role;



import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.interfaces.sys_role.RoleMenuService;

import com.intellagric.mapper.SysOperateMapper;
import com.intellagric.mapper.SysRoleMenuMapper;
import com.intellagric.mapper.SysRoleOperateMapper;
import com.intellagric.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/13
 * @DESC： 角色菜单id信息服务实现
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysOperateMapper sysOperateMapper;
    @Autowired
    private SysRoleOperateMapper sysRoleOperateMapper;


    /**
     * 增加角色id和菜单id
     * @param sysRoleMenuKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addRoleMenu(SysRoleMenuKey sysRoleMenuKey) {
        if (sysRoleMenuMapper.insertSelective(sysRoleMenuKey) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过角色id删除角色-菜单信息表
     * @param roleId
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteRoleMenuByRoleId(String roleId) {
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        if (sysRoleMenuMapper.deleteByExample(sysRoleMenuExample) != 0) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改角色id和菜单id
     * @param sysRoleMenuKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editRoleMenu(SysRoleMenuKey sysRoleMenuKey) {
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(sysRoleMenuKey.getRoleId());
        sysRoleMenuMapper.updateByExample(sysRoleMenuKey,sysRoleMenuExample);
        if (sysRoleMenuMapper.updateByExample(sysRoleMenuKey,sysRoleMenuExample) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询权限列表
     * @param roleId
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getRoleMenuByRoleId(String roleId) {
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        List<SysRoleMenuKey> roleMenuKeys = sysRoleMenuMapper.selectByExample(sysRoleMenuExample);
        return LayuiDataGridResult.success().add(roleMenuKeys,roleMenuKeys.size());
    }

    /**
     * 查询权限列表
     * @param roleId
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getOperatesByRoleId(String roleId) {
        SysRoleOperateExample sysRoleOperateExample=new SysRoleOperateExample();
        sysRoleOperateExample.createCriteria().andRoleIdEqualTo(roleId);
        List<SysRoleOperateKey> roleOperateKeys = sysRoleOperateMapper.selectByExample(sysRoleOperateExample);

        List<SysOperate> operateList=new ArrayList<>();
        for(SysRoleOperateKey rks:roleOperateKeys) {
            SysOperateExample sysOperateExample = new SysOperateExample();
            sysOperateExample.createCriteria().andIdEqualTo(rks.getOperateId());
            List<SysOperate> operates = sysOperateMapper.selectByExample(sysOperateExample);
            operateList.add(operates.get(0));
        }

        List<SysOperate> rootSysOperateList = this.getOperateByParentId("0").getData();

        //生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
        for (SysOperate sysOperate : rootSysOperateList) {
            sysOperate.setChildren(getChild(sysOperate.getId(), operateList));
        }


        return LayuiDataGridResult.success().add(rootSysOperateList,roleOperateKeys.size());
    }

    private List<SysOperate> getChild(String id, List<SysOperate> operateList) {
        // 子菜单
        List<SysOperate> childList = new ArrayList<SysOperate>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (SysOperate sysOperate : operateList) {
            if (sysOperate.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (sysOperate.getParentId().equals(id)) {
                    childList.add(sysOperate);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (SysOperate sysOperate : childList) {
            // 递归
            sysOperate.setChildren(getChild(sysOperate.getId(), operateList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    public LayuiDataGridResult getOperateByParentId(String parentId) {
        SysOperateExample sysOperateExample = new SysOperateExample();

        sysOperateExample.setOrderByClause("sort DESC");//以sort字段进行排序
        sysOperateExample.createCriteria().andParentIdEqualTo(parentId);
        List<SysOperate> rootSysOperateList = sysOperateMapper.selectByExample(sysOperateExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootSysOperateList);
        return result;
    }


}
